{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Absolute Difference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minimumAbsDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最小绝对差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你个整数数组&nbsp;<code>arr</code>，其中每个元素都 <strong>不相同</strong>。</p>\n",
    "\n",
    "<p>请你找到所有具有最小绝对差的元素对，并且按升序的顺序返回。</p>\n",
    "\n",
    "<p>每对元素对 <code>[a,b</code>] 如下：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>a ,&nbsp;b</code>&nbsp;均为数组&nbsp;<code>arr</code>&nbsp;中的元素</li>\n",
    "\t<li><code>a &lt; b</code></li>\n",
    "\t<li><code>b - a</code>&nbsp;等于 <code>arr</code> 中任意两个元素的最小绝对差</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [4,2,1,3]\n",
    "<strong>输出：</strong>[[1,2],[2,3],[3,4]]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [1,3,6,10,15]\n",
    "<strong>输出：</strong>[[1,3]]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [3,8,-10,23,19,-4,-14,27]\n",
    "<strong>输出：</strong>[[-14,-10],[19,23],[23,27]]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= arr.length &lt;= 10^5</code></li>\n",
    "\t<li><code>-10^6 &lt;= arr[i] &lt;= 10^6</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-absolute-difference](https://leetcode.cn/problems/minimum-absolute-difference/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-absolute-difference](https://leetcode.cn/problems/minimum-absolute-difference/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[4,2,1,3]', '[1,3,6,10,15]', '[3,8,-10,23,19,-4,-14,27]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        res = []\n",
    "        min_diff = arr[1] - arr[0]\n",
    "        for i in range(1, len(arr)):\n",
    "            left, right = arr[i-1], arr[i]\n",
    "            if right - left < min_diff:\n",
    "                res = [[left, right]]\n",
    "                min_diff = right - left\n",
    "            elif right - left == min_diff:\n",
    "                res.append([left, right])\n",
    "            else:\n",
    "                continue\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        n = len(arr)\n",
    "        arr.sort()\n",
    "        s = inf\n",
    "        for i in range(n-1):\n",
    "            if arr[i+1] - arr[i] < s:\n",
    "                s = arr[i+1] - arr[i]\n",
    "                ans = []\n",
    "            if arr[i+1] - arr[i] == s:\n",
    "                ans.append([arr[i], arr[i+1]])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        d=inf\n",
    "        l=[]\n",
    "        arr.sort()\n",
    "        n=len(arr)\n",
    "        for i in range(1,n):\n",
    "            if arr[i]-arr[i-1]<d:\n",
    "                d=arr[i]-arr[i-1]\n",
    "                l=[[arr[i-1],arr[i]]]\n",
    "            elif arr[i]-arr[i-1]==d:\n",
    "                l.append([arr[i-1],arr[i]])\n",
    "        return l\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr = sorted(arr)\n",
    "        min_dif = float('inf')\n",
    "        ans = []\n",
    "        for i in range(len(arr)-1):\n",
    "            cur_dif = arr[i+1] - arr[i]\n",
    "            if cur_dif < min_dif:\n",
    "                ans = [[arr[i], arr[i+1]]]\n",
    "                min_dif = cur_dif\n",
    "            elif cur_dif == min_dif:\n",
    "                ans.append([arr[i], arr[i+1]])\n",
    "        return ans\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        n = len(arr)\n",
    "        arr.sort()\n",
    "\n",
    "        best, ans = float('inf'), list()\n",
    "        for i in range(n - 1):\n",
    "            if (delta := arr[i + 1] - arr[i]) < best:\n",
    "                best = delta\n",
    "                ans = [[arr[i], arr[i + 1]]]\n",
    "            elif delta == best:\n",
    "                ans.append([arr[i], arr[i + 1]])\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        mini_diff = min([y - x for x, y in zip(arr[:-1], arr[1:])])\n",
    "        res = []\n",
    "\n",
    "        for i in range(len(arr) - 1):\n",
    "            if arr[i + 1] - arr[i] == mini_diff:\n",
    "                res.append([arr[i], arr[i + 1]])\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_abs = inf\n",
    "        n = len(arr)\n",
    "        hax = collections.defaultdict(list)\n",
    "        for i in range(n-1):\n",
    "            temp = arr[i+1]-arr[i]\n",
    "            min_abs = min(temp, min_abs)\n",
    "            hax[temp].append(i)\n",
    "        return [[arr[i], arr[i+1]] for i in hax[min_abs]]\n",
    "\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_abs = inf\n",
    "        n = len(arr)\n",
    "        hax = collections.defaultdict(list)\n",
    "        for i in range(n-1):\n",
    "            temp = arr[i+1]-arr[i]\n",
    "            min_abs = min(temp, min_abs)\n",
    "            hax[temp].append(i)\n",
    "        return [[arr[i], arr[i+1]] for i in hax[min_abs]]\n",
    "\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr = sorted(arr)\n",
    "        _min = 1000000\n",
    "        res = []\n",
    "        for i,j in zip(arr[:-1],arr[1:]):\n",
    "            if _min < j-i:\n",
    "                continue\n",
    "            elif _min == j-i:\n",
    "                res.append([i,j])\n",
    "            else:\n",
    "                _min = j-i\n",
    "                res = []\n",
    "                res.append([i,j])\n",
    "        return res\n",
    "            \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr=sorted(arr)\n",
    "        m = 99999\n",
    "        res=[]\n",
    "        for i in range(len(arr)-1):\n",
    "            m = min(m,abs(arr[i]-arr[i+1]))\n",
    "        print(m,'m',arr)\n",
    "        for i in range(len(arr)-1):\n",
    "            if abs(arr[i]-arr[i+1])==m:\n",
    "                res.append([arr[i],arr[i+1]])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "\n",
    "        def mergeSort(arr):\n",
    "            n = len(arr)\n",
    "            if n < 2:\n",
    "                return arr\n",
    "            left = mergeSort(arr[:n//2])\n",
    "            right = mergeSort(arr[n//2:])\n",
    "            rst = []\n",
    "            while len(left)>0 and len(right)>0:\n",
    "                if left[0] > right[0]:\n",
    "                    rst.append(right.pop(0))\n",
    "                else:\n",
    "                    rst.append(left.pop(0))\n",
    "            for e in left:\n",
    "                rst.append(e)\n",
    "            for e in right:\n",
    "                rst.append(e)\n",
    "            return rst\n",
    "        arr = mergeSort(arr)\n",
    "\n",
    "        arr.sort(reverse=False)\n",
    "\n",
    "        rst = [[arr[0], arr[1]]]\n",
    "        min_value = abs(arr[1] - arr[0])\n",
    "        for i in range(1, len(arr)-1):\n",
    "            if abs(arr[i+1] - arr[i]) < min_value:\n",
    "                rst = [[arr[i], arr[i+1]]]\n",
    "                min_value = abs(arr[i+1] - arr[i])\n",
    "            elif abs(arr[i+1] - arr[i]) == min_value:\n",
    "                rst.append([arr[i], arr[i+1]])\n",
    "        return rst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        \n",
    "        def binary_sort(li):\n",
    "            if len(li) <= 1:\n",
    "                return li\n",
    "            pivot = li[0]\n",
    "            less = [num for num in li[1:] if num <= pivot]\n",
    "            great = [num for num in li[1:] if num > pivot]\n",
    "            return binary_sort(less) + [pivot] + binary_sort(great)\n",
    "        \n",
    "        arr = binary_sort(arr)\n",
    "\n",
    "        #cha = min([arr[i]-arr[i-1] for i in range(1,len(arr))])\n",
    "        \n",
    "        res = [[arr[0],arr[1]]]\n",
    "        cha = arr[1] - arr[0]\n",
    "        for i in range(2,len(arr)):\n",
    "            if arr[i] - arr[i-1] < cha:\n",
    "                cha = arr[i] - arr[i-1]\n",
    "                res= [[arr[i-1],arr[i]]]\n",
    "            elif arr[i] - arr[i-1] == cha:\n",
    "                res.append([arr[i-1],arr[i]])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        \n",
    "        #定义二分排序\n",
    "        def binary_sort(li):\n",
    "            if len(li) <= 1:\n",
    "                return li\n",
    "            pivot = li[0]\n",
    "            less = [num for num in li[1:] if num <= pivot]\n",
    "            great = [num for num in li[1:] if num > pivot]\n",
    "            return binary_sort(less) + [pivot] + binary_sort(great)\n",
    "        \n",
    "        #调用二分排序\n",
    "        arr = binary_sort(arr)\n",
    "\n",
    "        #遍历一遍\n",
    "        res = [[arr[0],arr[1]]]\n",
    "        cha = arr[1] - arr[0]\n",
    "        for i in range(2,len(arr)):\n",
    "            if arr[i] - arr[i-1] < cha:\n",
    "                cha = arr[i] - arr[i-1]\n",
    "                res= [[arr[i-1],arr[i]]]\n",
    "            elif arr[i] - arr[i-1] == cha:\n",
    "                res.append([arr[i-1],arr[i]])\n",
    "        \n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        \n",
    "        def binary_sort(li):\n",
    "            if len(li) <= 1:\n",
    "                return li\n",
    "            pivot = li[0]\n",
    "            less = [num for num in li[1:] if num <= pivot]\n",
    "            great = [num for num in li[1:] if num > pivot]\n",
    "            return binary_sort(less) + [pivot] + binary_sort(great)\n",
    "        \n",
    "        arr = binary_sort(arr)\n",
    "\n",
    "        #cha = min([arr[i]-arr[i-1] for i in range(1,len(arr))])\n",
    "        \n",
    "        res = [[arr[0],arr[1]]]\n",
    "        cha = arr[1] - arr[0]\n",
    "        for i in range(2,len(arr)):\n",
    "            if arr[i] - arr[i-1] < cha:\n",
    "                cha = arr[i] - arr[i-1]\n",
    "                res= [[arr[i-1],arr[i]]]\n",
    "            elif arr[i] - arr[i-1] == cha:\n",
    "                res.append([arr[i-1],arr[i]])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        '''\n",
    "        # 我的\n",
    "        arr.sort()\n",
    "        ret = []\n",
    "        val = float('inf')\n",
    "        for i in range(len(arr) - 1):\n",
    "            if arr[i+1] - arr[i] == val:\n",
    "                ret.append([arr[i], arr[i+1]])\n",
    "            elif arr[i+1] - arr[i] < val:\n",
    "                ret.clear()\n",
    "                ret.append([arr[i], arr[i+1]])\n",
    "                val = arr[i+1] - arr[i]\n",
    "        return ret\n",
    "        '''\n",
    "        \n",
    "        # 别人的 巧妙的思路\n",
    "        \n",
    "        arr_set = set(arr)\n",
    "        dist, result = 0,  []\n",
    "        \n",
    "        while not result:\n",
    "            dist += 1\n",
    "            result = [x for x in arr if x + dist in arr_set]\n",
    "            \n",
    "        return [[x, x + dist] for x in sorted(result)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        if arr == None:\n",
    "            return []\n",
    "        \n",
    "        arr.sort()\n",
    "        res = []\n",
    "        for i in range(1, len(arr)):            \n",
    "            res.append([abs(arr[i] - arr[i-1]), arr[i-1], arr[i]])\n",
    "        \n",
    "        res.sort(key=lambda x: x[0])\n",
    "\n",
    "        res1 = []\n",
    "        res1.append([res[0][1], res[0][2]])        \n",
    "        for i in range(1, len(res)):\n",
    "            if res[i][0] > res[i-1][0]:\n",
    "                break\n",
    "            else:\n",
    "                res1.append([res[i][1], res[i][2]])\n",
    "        \n",
    "        return res1        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        def quicksort(s):\n",
    "            if len(s) < 2:\n",
    "                return s\n",
    "            pivot = s[0]\n",
    "            left = [x for x in s[1:] if x<=pivot]\n",
    "            right = [x for x in s[1:] if x>pivot]\n",
    "\n",
    "            retarr = quicksort(left) + [pivot] + quicksort(right)\n",
    "            return retarr\n",
    "        \n",
    "        arr = quicksort(arr)\n",
    "        ret = []\n",
    "        low = math.inf\n",
    "\n",
    "        for i in range(len(arr)-1):\n",
    "            v = arr[i+1] - arr[i]\n",
    "            if v == low:\n",
    "                ret.append([arr[i],arr[i+1]])\n",
    "            elif v < low:\n",
    "                ret = [[arr[i],arr[i+1]]]\n",
    "                low = v\n",
    "            \n",
    "        return ret\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        N = len(arr)\n",
    "        min_gap = min(arr[i] - arr[i - 1] for i in range(1, N))\n",
    "        ans = []\n",
    "        arr_set=set(arr)\n",
    "        for v in arr:\n",
    "            if v + min_gap in arr_set:\n",
    "                ans.append([v, v + min_gap])\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def get_abs_val(self, x):\n",
    "        return x[0]\n",
    "\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        if arr == None:\n",
    "            return []\n",
    "        \n",
    "        arr.sort()\n",
    "        res = []\n",
    "        for i in range(1, len(arr)):            \n",
    "            res.append([abs(arr[i] - arr[i-1]), arr[i-1], arr[i]])\n",
    "        \n",
    "        #res.sort(key=self.get_abs_val)\n",
    "        res.sort(key=lambda x: x[0])\n",
    "\n",
    "        res1 = []\n",
    "        res1.append([res[0][1], res[0][2]])        \n",
    "        for i in range(1, len(res)):\n",
    "            if res[i][0] > res[i-1][0]:\n",
    "                break\n",
    "            else:\n",
    "                res1.append([res[i][1], res[i][2]])\n",
    "        \n",
    "        return res1        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr_set = set(arr)\n",
    "        sorted_arr = sorted(arr)\n",
    "        min_diff = float('inf')\n",
    "        for i in range(len(arr)-1):\n",
    "            min_diff = min(min_diff, sorted_arr[i+1]-sorted_arr[i])\n",
    "        res = []\n",
    "        for key in sorted_arr:\n",
    "            if (key - min_diff) in arr_set:\n",
    "                res.append([key, key-min_diff])\n",
    "            if (key + min_diff) in arr_set:\n",
    "                res.append([key, key+min_diff])\n",
    "            arr_set.remove(key)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        pair_arr = list(zip(arr[:-1], arr[1:]))\n",
    "        sub_arr = [abs(x[0]-x[1]) for x in pair_arr]\n",
    "        min_sub = min(sub_arr)\n",
    "\n",
    "        result = []\n",
    "        for idx, v in enumerate(sub_arr):\n",
    "            if v == min_sub:\n",
    "                result.append(list(pair_arr[idx]))\n",
    "\n",
    "        # print(result)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        s = set(arr)\n",
    "        res = []\n",
    "        d = 0\n",
    "        while not res:\n",
    "            d += 1\n",
    "            res = [e for e in s if e + d in s]\n",
    "        return [[e, e+d] for e in sorted(res)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        min_diff = math.inf\n",
    "        arr.sort()\n",
    "        for i in range(1, len(arr)):\n",
    "            min_diff = min(min_diff, arr[i]-arr[i-1])\n",
    "        ans = []\n",
    "        nums =set(arr)\n",
    "        for i in nums:\n",
    "            if i+min_diff in nums:\n",
    "                ans.append([i,  i+min_diff ])\n",
    "        return sorted(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        s = set(arr)\n",
    "        res = []\n",
    "        d = 0\n",
    "        while not res:\n",
    "            d += 1\n",
    "            res = [e for e in s if e + d in s]\n",
    "        return [[e, e + d] for e in sorted(res)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr_sort = sorted(arr)\n",
    "        min_difference = collections.defaultdict(set)\n",
    "        last_diff = float('inf')\n",
    "        last_items = (0, 0)\n",
    "        for i in range(len(arr_sort)):\n",
    "            if i < len(arr_sort)-1:\n",
    "                diff = abs(arr_sort[i]-arr_sort[i+1])\n",
    "                items = tuple(sorted(arr_sort[i: i+2]))\n",
    "                if diff < last_diff:\n",
    "                    min_difference[diff].add(items)\n",
    "                elif diff == last_diff:\n",
    "                    min_difference[last_diff].add(last_items)\n",
    "                    min_difference[diff].add(items)\n",
    "                else:\n",
    "                    min_difference[last_diff].add(last_items)\n",
    "                last_diff = diff\n",
    "                last_items = items\n",
    "            else:\n",
    "                min_difference[last_diff].add(last_items)\n",
    "        min_diff = min(min_difference.keys())\n",
    "        return sorted([list(item) for item in min_difference[min_diff]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_diff = float('inf')\n",
    "        for i in range(1, len(arr)):\n",
    "            min_diff = min(min_diff, arr[i] - arr[i - 1])\n",
    "        \n",
    "        nums = set(arr)\n",
    "        ans = []\n",
    "        for i in arr:\n",
    "            if i + min_diff in nums:\n",
    "               ans.append([i, i+min_diff]) \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        a = sorted(arr)\n",
    "        b =[]\n",
    "        c =[]\n",
    "        m = float('inf') \n",
    "        for i in range(len(a)-1):\n",
    "            d = a[i+1]-a[i]\n",
    "            m = min(m,d)\n",
    "            b.append([a[i],a[i+1],d])     \n",
    "        for i, j, k in b:\n",
    "            if k == m:\n",
    "                c.append([i,j])\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        t = [[i,j] for i,j in zip(arr,arr[1:])]\n",
    "        m = min(j-i for i,j in t)\n",
    "        return [[i,j] for i,j in t if j-i==m]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        b = set(arr)\n",
    "        res = []\n",
    "        flag = 0\n",
    "        while not res:\n",
    "            flag += 1\n",
    "            res = [e for e in arr if e + flag in b]\n",
    "        return [[e, e + flag] for e in sorted(res)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        seti,diff,ans=set(arr),0,[]\n",
    "        while not ans:\n",
    "            diff+=1\n",
    "            for n in seti:\n",
    "                if n+diff in seti:ans.append(n)\n",
    "        return [[e,e+diff] for e in sorted(ans)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        s = set(arr)\n",
    "        res = []\n",
    "        d = 0\n",
    "        while not res:\n",
    "            d += 1\n",
    "            res = [e for e in s if e + d in s]\n",
    "        return [[e, e + d] for e in sorted(res)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        s = set(arr)\n",
    "        arr.sort()\n",
    "\n",
    "        # 拿到最小绝对差的值\n",
    "        min_abs = float(\"inf\")\n",
    "        for i in range(1, len(arr)):\n",
    "            min_abs = min(abs(arr[i] - arr[i - 1]), min_abs)\n",
    "\n",
    "        ans = []\n",
    "        for num in arr:\n",
    "            if num + min_abs in s:\n",
    "                ans.append([num, num + min_abs])\n",
    "        return ans \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        gap = math.inf\n",
    "\n",
    "        # 打擂台获得gap\n",
    "        for idx, val in enumerate(arr):\n",
    "            if idx == 0:\n",
    "                continue\n",
    "            \n",
    "            gap = min(gap, val-arr[idx-1])\n",
    "        \n",
    "        res = []\n",
    "        hash_set = set(arr)\n",
    "        for val in arr:\n",
    "            if val + gap not in hash_set:\n",
    "                continue\n",
    "            res.append([val, val+gap])\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_val = float('inf')\n",
    "        abs_result = []\n",
    "        result = []\n",
    "        for i in range(1, len(arr)):\n",
    "            abs_val = abs(arr[i] - arr[i - 1])\n",
    "            abs_result.append((abs_val, i))\n",
    "            min_val = min(min_val, abs_val)\n",
    "\n",
    "        for comb in abs_result:\n",
    "            if comb[0] == min_val:\n",
    "                result.append([arr[comb[1]-1], arr[comb[1]]])\n",
    "        \n",
    "        return result\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_val = float('inf')\n",
    "        abs_result = []\n",
    "        result = []\n",
    "        for i in range(1, len(arr)):\n",
    "            abs_val = abs(arr[i] - arr[i - 1])\n",
    "            abs_result.append((abs_val, i))\n",
    "            min_val = min(min_val, abs_val)\n",
    "\n",
    "        for comb in abs_result:\n",
    "            if comb[0] == min_val:\n",
    "                result.append([arr[comb[1]-1], arr[comb[1]]])\n",
    "        \n",
    "        return result\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_diff=float('inf')\n",
    "        for i in range(1,len(arr)):\n",
    "            min_diff=min((arr[i]-arr[i-1]),min_diff)\n",
    "        res=set()\n",
    "        st=set(arr)\n",
    "        for i in range(len(arr)):\n",
    "            a=arr[i]-min_diff\n",
    "            b=arr[i]+min_diff\n",
    "            if a in st:\n",
    "                res.add((a,arr[i]))\n",
    "            if b in st:\n",
    "                res.add((arr[i],b))\n",
    "        res=[list(i) for i in res]\n",
    "        res.sort()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        gapdic={}\n",
    "        for i in range(1,len(arr)):\n",
    "            gap=arr[i]-arr[i-1]\n",
    "            if gap not in gapdic:\n",
    "                gapdic[gap]=[[arr[i-1],arr[i]]]\n",
    "            else:\n",
    "                gapdic[gap].append([arr[i-1],arr[i]])\n",
    "        # print(min(gapdic))\n",
    "        # gaplist=list(gapdic.keys())\n",
    "        return gapdic[min(gapdic)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        temp = sorted(arr)\n",
    "        diff = 10**7\n",
    "        for i in range(1, len(temp)):\n",
    "            diff = min(diff, temp[i]-temp[i-1])\n",
    "        sset = set(arr)\n",
    "        ans = []\n",
    "        sset_1 = set()\n",
    "        for x in arr:\n",
    "            if x+diff in sset and (x, x+diff) not in sset_1 and (x+diff, x) not in sset_1:\n",
    "                ans.append([x, x+diff])\n",
    "                sset_1.add((x,x+diff))\n",
    "                sset_1.add((x+diff, x))\n",
    "            elif x - diff in sset and () and (x, x-diff) not in sset_1 and (x-diff, x) not in sset_1:\n",
    "                ans.append([x, x-diff])\n",
    "                sset_1.add((x-diff, x))\n",
    "                sset_1.add((x, x-diff))\n",
    "        return sorted(ans) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        z = list(zip(arr, arr[1:]))\n",
    "        d = min(y - x for x, y in z)\n",
    "        return [list(x) for x in z if x[1] - x[0] == d]\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        pair = list(zip(arr, arr[1:]))\n",
    "        mingap = 10**7\n",
    "        for i,j in pair:\n",
    "            if abs(i-j)<mingap:mingap=abs(i-j)\n",
    "        ans = [[i,j] for i,j in pair if abs(i-j)==mingap]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        pairs = list(zip(arr[:],arr[1:]))\n",
    "        diff = [y-x for x, y in  pairs]\n",
    "        Mindiff = min(diff)\n",
    "        res = []\n",
    "        for i,d in enumerate(diff):\n",
    "            if d==Mindiff:\n",
    "                res.append(pairs[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        a = sorted(arr)\n",
    "        o = []\n",
    "        b = inf\n",
    "        for i in range(len(a) - 1):\n",
    "            c = a[i + 1] - a[i]\n",
    "            o.append([a[i], a[i + 1], c])\n",
    "            if c < b:\n",
    "                b = c\n",
    "        q = [[i[0], i[1]] for i in o if i[2] == b]\n",
    "        return(q)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        list1=[]\n",
    "        list2=[]\n",
    "        n=len(arr)\n",
    "        for i in range(n-1):\n",
    "            temp=[]\n",
    "            temp.append(arr[i])\n",
    "            temp.append(arr[i+1])\n",
    "            list2.append(arr[i+1]-arr[i])\n",
    "            temp.append(list2[-1])\n",
    "            list1.append(temp)\n",
    "        ans=min(list2)\n",
    "        res=[]\n",
    "        for i in list1:\n",
    "            if i[2]==ans:\n",
    "                res.append(i[:2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        mn_d = inf\n",
    "        for i in range(len(arr) - 1):\n",
    "            mn_d = min(mn_d, abs(arr[i] - arr[i+1]))\n",
    "        s = set(arr)\n",
    "        ans = []\n",
    "        for x in s:\n",
    "            if x - mn_d in s:\n",
    "                ans.append([x - mn_d, x])\n",
    "        return sorted(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        L = [[arr[i], arr[i + 1]] for i in range(len(arr) - 1)]\n",
    "        x, ans = float('inf'), []\n",
    "        for i in L:\n",
    "            if i[1] - i[0] < x:\n",
    "                ans = []\n",
    "                x = i[1] - i[0]\n",
    "                ans.append(i)\n",
    "            elif i[1] - i[0] == x:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        hash = {}\n",
    "        n = len(arr)\n",
    "        arr.sort()\n",
    "        for i in range(n - 1):\n",
    "            diff = arr[i + 1] - arr[i]\n",
    "            if diff not in hash:\n",
    "                hash[diff] = []\n",
    "            hash[diff].append([arr[i], arr[i + 1]])\n",
    "        return hash[min(hash.keys())]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        hash = {}\n",
    "        n = len(arr)\n",
    "        arr.sort()\n",
    "        for i in range(n - 1):\n",
    "            diff = arr[i + 1] - arr[i]\n",
    "            if diff not in hash:\n",
    "                hash[diff] = []\n",
    "            hash[diff].append([arr[i], arr[i + 1]])\n",
    "        return hash[min(hash.keys())]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        result={}\n",
    "        for i in range(len(arr)-1):\n",
    "            redu=arr[i+1]-arr[i]\n",
    "            if redu not in result:\n",
    "                result[redu]=[[arr[i],arr[i+1]]]\n",
    "            else:\n",
    "                result[redu].append([arr[i],arr[i+1]])\n",
    "        return result.get(min(result.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        #sort\n",
    "        arr.sort()\n",
    "        diff_dict=defaultdict(list) #{diff:[[1,2],[],[]]}\n",
    "        mini_diff=float('inf')\n",
    "\n",
    "        #record the diff\n",
    "        for i in range(len(arr)-1):\n",
    "            diff=arr[i+1]-arr[i]\n",
    "            mini_diff=min(diff, mini_diff)\n",
    "            diff_dict[diff].append([arr[i],arr[i+1]])\n",
    "        return diff_dict[mini_diff]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr = sorted(arr)\n",
    "        ans_map = {}\n",
    "        for i in range(len(arr)-1):\n",
    "            pre = arr[i]\n",
    "            aft = arr[i+1]\n",
    "            if not aft-pre in ans_map:\n",
    "                ans_map[aft-pre] = [[pre,aft]]\n",
    "            else:\n",
    "                ans_map[aft-pre].append([pre,aft])\n",
    "        return ans_map[min(list(ans_map.keys()))]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_diff = {}\n",
    "        min_val = []\n",
    "        for i in range(1, len(arr)):\n",
    "            tmp_ans = arr[i] - arr[i - 1]\n",
    "            min_val.append(tmp_ans)\n",
    "            if tmp_ans not in min_diff:\n",
    "                min_diff[tmp_ans] = [[arr[i - 1], arr[i]]]\n",
    "            else:\n",
    "                min_diff[tmp_ans] = min_diff[tmp_ans] + [[arr[i - 1], arr[i]]]\n",
    "        return min_diff[min(min_val)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        min_diff = {}\n",
    "        min_val = []\n",
    "        for i in range(1, len(arr)):\n",
    "            tmp_ans = arr[i] - arr[i - 1]\n",
    "            min_val.append(tmp_ans)\n",
    "            if tmp_ans not in min_diff:\n",
    "                min_diff[tmp_ans] = [[arr[i - 1], arr[i]]]\n",
    "            else:\n",
    "                min_diff[tmp_ans] = min_diff[tmp_ans] + [[arr[i - 1], arr[i]]]\n",
    "        return min_diff[min(min_val)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        list1=[]\n",
    "        arr.sort()\n",
    "        for i in range(len(arr)-1):\n",
    "            list1.append([arr[i],arr[i+1]])\n",
    "        list2=[]\n",
    "        for i in list1:\n",
    "            list2.append(i[1]-i[0])\n",
    "        a=min(list2)\n",
    "        list3=[]\n",
    "        for i in list1:\n",
    "            if i[1]-i[0]==a:\n",
    "                list3.append(i)\n",
    "        return list3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sortedcontainers import SortedDict\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        collector = SortedDict()\n",
    "        n = len(arr)\n",
    "        for i in range(n-1):\n",
    "            delta = arr[i+1] - arr[i]\n",
    "            if delta not in collector:\n",
    "                collector[delta] = list()\n",
    "            collector[delta].append([arr[i], arr[i+1]])\n",
    "        for key in collector:\n",
    "            return collector[key]\n",
    "        return list()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        pairs = []\n",
    "        diffs = []\n",
    "        results = []\n",
    "        n=len(arr)\n",
    "        i=0\n",
    "        j=1\n",
    "        while j<n:\n",
    "            diffs.append(arr[j]-arr[i])\n",
    "            pairs.append([arr[i],arr[j]])\n",
    "            i=i+1\n",
    "            j=j+1\n",
    "        min_diff = min(diffs)\n",
    "        for i in range(len(diffs)):\n",
    "            if diffs[i]==min_diff:\n",
    "                results.append(pairs[i])\n",
    "        return results\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        nums = sorted(arr)\n",
    "        record = defaultdict(list)\n",
    "        for i,num in enumerate(nums[:-1]):\n",
    "            diff = nums[i+1] - num\n",
    "            record[diff].append([num,nums[i+1]])\n",
    "        min_diff,min_pairs = min((diff,pairs) for (diff,pairs) in record.items())\n",
    "        return min_pairs\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        list1=[]\n",
    "        n=len(arr)\n",
    "        for i in range(n-1):\n",
    "            list1.append([arr[i],arr[i+1]])\n",
    "        list1.sort(key=lambda x:x[1]-x[0])\n",
    "        ans=list1[0][1]-list1[0][0]\n",
    "        res=[]\n",
    "        for i in list1:\n",
    "            if i[1]-i[0]>ans:\n",
    "                break\n",
    "            res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        arr=[[arr[i-1],arr[i]] for i in range(1,len(arr))]\n",
    "        arr.sort(key=lambda x:x[1]-x[0])\n",
    "        mi=arr[0][1]-arr[0][0]\n",
    "        i=0\n",
    "        while i<len(arr):\n",
    "            if arr[i][1]-arr[i][0]!=mi:\n",
    "                break\n",
    "            i+=1\n",
    "        return arr[:i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr = sorted(arr)\n",
    "        pairs = [[i, ip] for i, ip in zip(arr, arr[1:])]\n",
    "        diff = [ip-i for i,ip in pairs]\n",
    "        min_diff = min(ip-i for i,ip in pairs)\n",
    "        res = [pair for pair in pairs if pair[1] - pair[0] == min_diff]\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        result = [[] for i in range(len(arr))]\n",
    "        temp = 0\n",
    "        min1 = abs(arr[0] - arr[1])\n",
    "        for i in range(len(arr) - 1):\n",
    "            if abs(arr[i] - arr[i+1]) > min1:\n",
    "                continue\n",
    "            if abs(arr[i] - arr[i+1]) < min1:\n",
    "                min1 = abs(arr[i] - arr[i+1])\n",
    "                result = [[] for i in range(len(arr))]\n",
    "                temp = 0\n",
    "                if arr[i] > arr[i + 1]:\n",
    "                    result[temp].append(arr[i+1])\n",
    "                    result[temp].append(arr[i])\n",
    "                else:\n",
    "                    result[temp].append(arr[i])\n",
    "                    result[temp].append(arr[i+1])\n",
    "                temp += 1\n",
    "                continue\n",
    "            if abs(arr[i] - arr[i + 1]) == min1:\n",
    "                if arr[i] > arr[i + 1]:\n",
    "                    result[temp].append(arr[i+1])\n",
    "                    result[temp].append(arr[i])\n",
    "                else:\n",
    "                    result[temp].append(arr[i])\n",
    "                    result[temp].append(arr[i+1])\n",
    "                temp += 1\n",
    "                continue\n",
    "        result = result[0:temp]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr = sorted(arr)\n",
    "        if len(arr) < 3:\n",
    "            return arr\n",
    "        temp_arr = []\n",
    "        temp_sum = []\n",
    "        for i in range(len(arr)):\n",
    "            j = i + 1\n",
    "            if j < len(arr):\n",
    "                temp_arr.append([arr[i], arr[j]])\n",
    "        for i in temp_arr:\n",
    "            temp_sum.append(i[1]-i[0])\n",
    "        min_value = min(temp_sum)\n",
    "\n",
    "        res = []\n",
    "        for i in temp_arr:\n",
    "            if i[1] - i[0] == min_value:\n",
    "                res.append(i)\n",
    "        return res\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        l=[]\n",
    "        c=[]\n",
    "        s=[]\n",
    "        for i in range(len(arr)-1):\n",
    "            l.append([abs(arr[i+1]-arr[i]),min(arr[i],arr[i+1]),max(arr[i],arr[i+1])])\n",
    "        for i in l:\n",
    "            c.append(i[0])\n",
    "        x=min(c)\n",
    "        for i in l:\n",
    "            if i[0]==x:\n",
    "                s.append([i[1],i[2]])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        minerror=math.inf\n",
    "        dic={}\n",
    "        for i in range(1,len(arr)):\n",
    "            error=arr[i]-arr[i-1]\n",
    "            minerror=min(error,minerror)\n",
    "            if error not in dic.keys():\n",
    "                dic[error]=[]\n",
    "                dic[error].append([arr[i-1],arr[i]])\n",
    "            else:\n",
    "                dic[error].append([arr[i-1],arr[i]])\n",
    "        return dic[minerror]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr=sorted(arr)\n",
    "        d=collections.defaultdict(list)\n",
    "        for i in range(len(arr)-1):\n",
    "            d[arr[i+1]-arr[i]].append([arr[i],arr[i+1]])\n",
    "        return d[min(d.keys())]\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        d = {}\n",
    "        i = 0\n",
    "        n = len(arr)-1\n",
    "        min_cha = arr[-1] - arr[0]\n",
    "        while i < n:\n",
    "            cha = arr[i+1] - arr[i]\n",
    "            if cha not in d:\n",
    "                d[cha] = [[arr[i],arr[i+1]]]\n",
    "                min_cha = min(min_cha,cha)\n",
    "            else:\n",
    "                d[cha].append([arr[i],arr[i+1]])\n",
    "            i += 1\n",
    "        return d[min_cha]\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        dist = {}\n",
    "        for i in range(len(arr)-1):\n",
    "            dist[i] = arr[i+1] - arr[i]\n",
    "        minDist = min(dist.values())\n",
    "        result = []\n",
    "        for i in dist:\n",
    "            if dist[i] == minDist:\n",
    "                tempt = [arr[i],arr[i+1]]\n",
    "                result.append(tempt)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        resk=[]\n",
    "        k1=[]\n",
    "        s=[]\n",
    "        arr=sorted(arr) #从小到大进行排序\n",
    "        for i in range(1,len(arr)):\n",
    "            res=[]\n",
    "            a=abs(arr[i]-arr[i-1])\n",
    "            res.append(arr[i-1])\n",
    "            res.append(arr[i])\n",
    "            resk.append(res)\n",
    "            k1.append(a)\n",
    "        b=min(k1)\n",
    "        for i in range(len(resk)):\n",
    "            if abs(resk[i][0]-resk[i][1])==b:\n",
    "                s.append(resk[i])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr = sorted(arr)\n",
    "        diff_dict = {}\n",
    "        diff_list = []\n",
    "        for index in range(len(arr) - 1):\n",
    "            diff = arr[index + 1] - arr[index]\n",
    "            diff_list.append(diff)\n",
    "            if diff not in diff_dict:\n",
    "                diff_dict[diff] = []\n",
    "                diff_dict[diff].append([arr[index], arr[index + 1]])\n",
    "            else:\n",
    "                diff_dict[diff].append([arr[index], arr[index + 1]])\n",
    "        diff_list = sorted(diff_list)\n",
    "        return diff_dict[diff_list[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        def takeSecond(elem):\n",
    "            return elem[1]\n",
    "        arr_sorted = sorted(arr)\n",
    "        new_arr = []\n",
    "        result_arr =[]\n",
    "        i = 0\n",
    "        while i < len(arr)-1:\n",
    "            new_arr.append(arr_sorted[i+1]-arr_sorted[i])\n",
    "            i += 1\n",
    "        new_arr_sorted = sorted(enumerate(new_arr),key=takeSecond)\n",
    "        print(new_arr_sorted)\n",
    "        k = 0\n",
    "        while k < len(new_arr_sorted):\n",
    "            if (new_arr_sorted[k][1] == new_arr_sorted[0][1]):\n",
    "                result_arr.append([arr_sorted[new_arr_sorted[k][0]],arr_sorted[new_arr_sorted[k][0]+1]])   \n",
    "            k += 1      \n",
    "        return result_arr       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        ret = sorted([[arr[i],arr[i+1]] for i in range(len(arr)-1)],key=lambda x:x[1]-x[0])\n",
    "        reta = ret[0][1] - ret[0][0]\n",
    "        return [i for i in ret if reta == i[1]-i[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        arr.sort()\n",
    "        ans = collections.defaultdict(list)\n",
    "        a = float(\"inf\")\n",
    "        for i in range(len(arr)-1):\n",
    "            b = abs(arr[i+1]-arr[i]) \n",
    "            a = min(b,a)\n",
    "            ans[b].append([arr[i],arr[i+1]])\n",
    "        return ans[a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        dc={}\n",
    "        arr.sort()\n",
    "        ls=len(arr)\n",
    "        rs=arr[-1]-arr[0]\n",
    "        for i in range(ls-1):\n",
    "            t=arr[i+1]-arr[i]\n",
    "            if  t in dc:\n",
    "                dc[t].append([arr[i],arr[i+1]])\n",
    "            else:\n",
    "                dc[t]=[[arr[i],arr[i+1]]]\n",
    "                rs=min(rs,t)\n",
    "        return dc[rs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:\n",
    "        a = deque()\n",
    "        b = deque()\n",
    "        d = []\n",
    "        arr.sort()\n",
    "        for i in range(1,len(arr)):\n",
    "            a.append(arr[i] - arr[i-1])\n",
    "            b.append([arr[i-1],arr[i]])\n",
    "        num = min(a)\n",
    "        for i in range(len(a)):\n",
    "            if a[i] == num:\n",
    "                d = d+[b[i]]\n",
    "        return d\n",
    "                "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
